/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package officeboost.domain;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import officeboost.entity.Group;
import officeboost.entity.User;
import officeboost.util.ObConstants;

/**
 *
 * @author sorra
 */
@Stateless
@LocalBean
public class PersonService {

    @PersistenceContext
    EntityManager em;
    @EJB
    LogMgr logger;
    @EJB
    GroupService groupService;

    public void testSetup() {        
        User test = new User();
        test.setId("student");
        test.setName("WangKail");  
        persist(test);
	logger.log("person persisted!");
    }

    // Create a user
    public void persist(User user) {
        if (user == null) {
            return;          
        }
        if (find(user.getId()) != null) {
            logger.log("person:" + user.getId() + "主键重复！");
            return;
        }
        em.persist(user);
    }

    // 更新User
     public void update(User user){
         em.merge(user);
     }
    
    // 通过userId查找User，找不到返回null
    public User find(String id) {
        return em.find(User.class, id);
    }

    // 按User查找
    public List<Group> findGroupsByUser(User user) {
        return user.getGroups();
    }

    // 某人请假
    public void leave(User user) {
        // 部门可以给假
        if (true) {
//           
           // user.setLeave(ObConstants.ISLEAVE);
            user.setTimeoff(ObConstants.ISLEAVE);
            em.merge(user);
        }
    }

    
    
    // 按组查看
    public List<User> findUsersByGroup(Group group){
        return group.getUsers();
    }
    
    // 查找所有用户
    public List<User> findAll() {
        Query query = em.createQuery("from User u");
        List<User> personResultAll = query.getResultList();
        if (personResultAll == null) {
            personResultAll = new ArrayList<>();
        }
        return personResultAll;
    }
    //
    public Group getGroup(String uid) {
	List<Group>groups = find(uid).getGroups();
	if (groups != null && !groups.isEmpty()) {
	    return groups.get(0);
	}	
	return null;
    }
    
    //用户登录
    public boolean checkLogin(String username, String password) {
        List<User> users = findAll();
	for (int i = 0; i < users.size(); i++) {
	    if (users.get(i).getId().equals(username) && /*users.get(i).getPassword()*/"123".equals(password)) {
		return true;
	    }
	}
	return false;
    }
    
     public boolean extractUser(String[] inputs) {
	if (inputs.length != 6) {
	    return false;
	}
        User user = new User();
	user.setId(inputs[0]);
	user.setPassword(inputs[1]);
        user.setName(inputs[2]);
        user.setGender(inputs[4]);
        user.setEmail(inputs[5]);
	
	persist(user);
	
        String[] groupNames = inputs[3].split(",");
        for (String gn : groupNames) {
	     Group group = groupService.getGroupByName(gn);
	     if (group != null) {
		group.getUsers().add(user);
	    }
	 }
	
	return true;
    }
    
}
